package com.mmd.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmd.bean.CODE;
import com.mmd.bean.UserLoginContext;
import com.mmd.common.entity.*;
import com.mmd.common.mapper.MmdQuotationDetailHistoryMapper;
import com.mmd.common.mapper.MmdQuotationHistoryMapper;
import com.mmd.exception.GameException;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.dao.*;
import com.mmd.manage.service.ModeMYCService;
import com.mmd.manage.service.QuotationService;
import com.mmd.manage.vo.QuotationEo;
import com.mmd.manage.vo.QuotationValidTimeVo;
import com.mmd.manage.vo.QuotationVo;
import com.mmd.manage.vo.RouteHscodeVo;
import com.mmd.utils.UniqueNoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author cyq
 * @Date 2019/8/29 20:20
 * @Description
 **/
@Service
public class QuotationServiceImpl implements QuotationService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MmdQuotationDao mmdQuotationDao;
    @Autowired
    private AreaDao areaDao;
    @Autowired
    private MmdHscodeDao mmdHscodeDao;
    @Autowired
    private MmdQuotationDetailDao mmdQuotationDetailDao;
    @Autowired
    private MmdQuotationHscodeDao mmdQuotationHscodeDao;
    @Autowired
    private LogDao logDao;
    @Autowired
    private ModeMYCService modeMYCService;
    @Autowired
    private MmdQuotationHistoryMapper mmdQuotationHistoryMapper;
    @Autowired
    private MmdQuotationDetailHistoryMapper mmdQuotationDetailHistoryMapper;

    @Override
    public List<MmdQuotation> getRouteNameList(String level, String channelNo, Long areaId) {
        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
        MmdArea mmdArea = areaDao.selectByPrimaryKey(areaId);
        if (mmdArea != null) {
            if (mmdArea.getLevel() == 2) {
                areaId = mmdArea.getParentAreaId();
            }
        }
        MmdQuotation mmdQuotation = new MmdQuotation();
        if (!StringUtils.isEmpty(channelNo)) {
            mmdQuotation.setChannelNo(channelNo);
        }
        if (areaId != null) {
            mmdQuotation.setAreaId(areaId);
        }
        mmdQuotation.setLevel(level);
//        List<MmdQuotation> mmdQuotations = mmdQuotationDao.selectByKeySelective(mmdQuotation);
        List<MmdQuotation> mmdQuotations = mmdQuotationDao.getQuotationGroupByRouteNo(mmdQuotation);
        for (MmdQuotation quotation : mmdQuotations) {
            StringBuilder sb = new StringBuilder();
            if ("admin".equals(userLoginContext.getLevel())) {
                sb.append(quotation.getLevel());
                sb.append("级").append(" > ");
            }
            sb.append(quotation.getAreaName());
            sb.append(" > ").append(quotation.getChannelName());
            sb.append(" > ").append(quotation.getRouteName()).toString();
            quotation.setRouteName(sb.toString());
        }
        return mmdQuotations;
    }

    /**
     * 校验routeNo的唯一性
     *
     * @param routeNo true 说明不存在
     */
    private boolean checkRouteNo(String routeNo, String quotationNo) {
        MmdQuotation mq = new MmdQuotation();
        mq.setRouteNo(routeNo);
        List<MmdQuotation> mmdQuotations = mmdQuotationDao.selectByKeySelective(mq);
        if (CollectionUtils.isEmpty(mmdQuotations)) {
            return true;
        }
        if (quotationNo.equals(mmdQuotations.get(0).getQuotationNo())) {
            return true;
        }

        return false;

    }


    @Override
    @Transactional
    public boolean addQquotation(QuotationEo eo) {
        logger.info("###########addQquotation param###########{}", JSON.toJSONString(eo));
        if (StringUtils.isEmpty(eo.getRouteNo())) {
            throw new GameException(new CODE(9999, "请填写渠道路线编码"));
        }
        if (StringUtils.isEmpty(eo.getRouteName())) {
            throw new GameException(new CODE(9999, "请填写渠道路线名称"));
        }
        if (eo.getChannelNo() == null) {
            throw new GameException(new CODE(9999, "请选择渠道"));
        }
        if (eo.getChannelName() == null) {
            throw new GameException(new CODE(9999, "请选择渠道"));
        }
        if (eo.getAreaId() == null) {
            throw new GameException(new CODE(9999, "请选择区域"));
        }
        if (StringUtils.isEmpty(eo.getDetailJson())) {
            throw new GameException(new CODE(9999, "请填写渠道路线详情"));
        }
        if (StringUtils.isEmpty(eo.getValidStartTimeStr())) {
            throw new GameException(new CODE(9999, "请填写有效开始时间"));
        }
        if (StringUtils.isEmpty(eo.getValidEndTimeStr())) {
            throw new GameException(new CODE(9999, "请填写有效结束时间"));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            eo.setValidStartTime(format.parse(eo.getValidStartTimeStr()));
            eo.setValidEndTime(format.parse(eo.getValidEndTimeStr()));
            logger.info("qqqq validStartTime:{}",eo.getValidStartTime());
        }catch (Exception e){
            throw new GameException(new CODE(9999, "有效开始时间或有效结束时间格式错误"));
        }

        String quotationNo = UniqueNoUtils.next(UniqueNoUtils.UniqueNoType.R);
//        if (!this.checkRouteNo(eo.getRouteNo(), quotationNo)) {
//            throw new GameException(new CODE(9999, "渠道路线编码已存在"));
//        }
        if (mmdQuotationDao.getQuotationNumberByTimeBetween(null, eo.getRouteNo(),eo.getValidStartTime(),eo.getValidEndTime()) > 0) {
            throw new GameException(new CODE(9999, "该有效时间内已存在报价单"));
        }

        //新增报价单
        MmdQuotation mq = new MmdQuotation();
        BeanUtils.copyProperties(eo, mq);
        mq.setQuotationNo(quotationNo);
        mq.setCreateTime(new Date());
        logger.info("mq validStartTime:{}",mq.getValidStartTime());
        mmdQuotationDao.insert(mq);
        //上面的更新不知道为啥不会更新有效时间
        mmdQuotationDao.updateValidTime(mq.getQuotationNo(), eo.getValidStartTime(), eo.getValidEndTime());

        //新增报价单详情
        List<Map> detailJsonArray = JSON.parseArray(eo.getDetailJson(), Map.class);
        MmdQuotationDetail mqd = null;
        List<MmdQuotationDetailHistory> detailHistoryList = new ArrayList<>();
        for (Map detailObject : detailJsonArray) {
            mqd = new MmdQuotationDetail();
            if (!"9".equals(eo.getChannelNo())) {
                BigDecimal kg21 = detailObject.get("kg21") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg21").toString());
                BigDecimal kg23 = detailObject.get("kg23") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg23").toString());
                BigDecimal kg46 = detailObject.get("kg46") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg46").toString());
                BigDecimal kg71 = detailObject.get("kg71") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg71").toString());
                BigDecimal kg100 = detailObject.get("kg100") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg100").toString());
                BigDecimal kg101 = detailObject.get("kg101") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg101").toString());
                BigDecimal kg301 = detailObject.get("kg301") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg301").toString());
                BigDecimal kg501 = detailObject.get("kg501") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg501").toString());
                BigDecimal kg1001 = detailObject.get("kg1001") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg1001").toString());

                mqd.setKg21(kg21);
                mqd.setKg23(kg23);
                mqd.setKg46(kg46);
                mqd.setKg71(kg71);
                mqd.setKg100(kg100);
                mqd.setKg101(kg101);
                mqd.setKg301(kg301);
                mqd.setKg501(kg501);
                mqd.setKg1001(kg1001);
            }else{
                BigDecimal cbm1 = detailObject.get("cbm1") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm1").toString());
                BigDecimal cbm2 = detailObject.get("cbm2") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm2").toString());
                BigDecimal cbm5 = detailObject.get("cbm5") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm5").toString());
                BigDecimal cbm10 = detailObject.get("cbm10") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm10").toString());

                mqd.setCbm1(cbm1);
                mqd.setCbm2(cbm2);
                mqd.setCbm5(cbm5);
                mqd.setCbm10(cbm10);
            }
            if (detailObject.get("drawTime") != null) {
                mqd.setDrawTime(detailObject.get("drawTime").toString());
            }

            mqd.setQuotationNo(quotationNo);
            mqd.setSonAreaId(Long.parseLong(detailObject.get("sonAreaId").toString()));
            mqd.setSonAreaName(detailObject.get("sonAreaName").toString());
            if (detailObject.get("areaRemark") != null) {
                mqd.setAreaRemark(detailObject.get("areaRemark").toString());
            }

            mmdQuotationDetailDao.insert(mqd);

            MmdQuotationDetailHistory mmdQuotationDetailHistory = new MmdQuotationDetailHistory();
            BeanUtils.copyProperties(mqd, mmdQuotationDetailHistory);
            detailHistoryList.add(mmdQuotationDetailHistory);
        }

        //插入附加价格
        if (!StringUtils.isEmpty(eo.getHscodeJson()) && !"null".equals(eo.getHscodeJson())) {
            logger.info("hscodeJson===>{}", eo.getHscodeJson());

            List<Map> hscodeJsonArray = JSON.parseArray(eo.getHscodeJson(), Map.class);
            MmdQuotationHscode mqh = null;
            for (Map hscodeObject : hscodeJsonArray) {
                if (hscodeObject.get("attachFee") != null) {
                    mqh = new MmdQuotationHscode();
                    BigDecimal attachFee = new BigDecimal(hscodeObject.get("attachFee").toString());
                    mqh.setQuotationNo(quotationNo);
                    mqh.setHscode(hscodeObject.get("hscode").toString());
                    mqh.setAttachFee(attachFee);
                    mmdQuotationHscodeDao.insert(mqh);
                }

            }
        }

        MmdQuotationHistory record = new MmdQuotationHistory();
        BeanUtils.copyProperties(mq, record);
        record.setCreateTime(new Date());
        mmdQuotationHistoryMapper.insert(record);
        for (MmdQuotationDetailHistory detailHistory : detailHistoryList){
            detailHistory.setHistoryId(record.getHistoryId());
            mmdQuotationDetailHistoryMapper.insert(detailHistory);
        }

        //添加日志
        MmdLog log = new MmdLog();
        log.setOperatorUid(AdminInterceptor.getUserLoginContext().getUid());
        log.setOperatorKey(quotationNo);
        log.setContent("新增报价单");
        log.setCreateTime(new Date());
        logDao.insertLog(log);
        return true;
    }

    @Override
    @Transactional
    public boolean modifyQuotation(QuotationEo eo) {
        logger.info("modifyQuotation 参数：{}", JSONObject.toJSONString(eo));
        if (StringUtils.isEmpty(eo.getRouteNo())) {
            throw new GameException(new CODE(9999, "请填写渠道路线编码"));
        }
        if (StringUtils.isEmpty(eo.getRouteName())) {
            throw new GameException(new CODE(9999, "请填写渠道路线名称"));
        }
        if (eo.getChannelNo() == null) {
            throw new GameException(new CODE(9999, "请选择渠道"));
        }
        if (eo.getAreaId() == null) {
            throw new GameException(new CODE(9999, "请选择区域"));
        }
        if (StringUtils.isEmpty(eo.getDetailJson())) {
            throw new GameException(new CODE(9999, "请填写渠道路线详情"));
        }
        if (StringUtils.isEmpty(eo.getValidStartTimeStr())) {
            throw new GameException(new CODE(9999, "请填写有效开始时间"));
        }
        if (StringUtils.isEmpty(eo.getValidEndTimeStr())) {
            throw new GameException(new CODE(9999, "请填写有效结束时间"));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            eo.setValidStartTime(format.parse(eo.getValidStartTimeStr()));
            eo.setValidEndTime(format.parse(eo.getValidEndTimeStr()));
            logger.info("11 validStartTimeStr:{}",eo.getValidStartTimeStr());
            logger.info("11 validStartTime:{}",eo.getValidStartTime());
        }catch (Exception e){
            throw new GameException(new CODE(9999, "有效开始时间或有效结束时间格式错误"));
        }

//        if (!this.checkRouteNo(eo.getRouteNo(), eo.getQuotationNo())) {
//            throw new GameException(new CODE(9999, "渠道路线编码已存在"));
//        }

        if (mmdQuotationDao.getQuotationNumberByTimeBetween(eo.getQuotationNo(), eo.getRouteNo(),eo.getValidStartTime(),eo.getValidEndTime()) > 0) {
            throw new GameException(new CODE(9999, "该有效时间内已存在报价单"));
        }

        //修改报价单
        MmdQuotation mq = new MmdQuotation();
        BeanUtils.copyProperties(eo, mq);
        mq.setUpdateTime(new Date());
        logger.info("mq validStartTime:{}",mq.getValidStartTime());
        mmdQuotationDao.updateByPrimaryKeySelective(mq);
        //上面的更新不知道为啥不会更新有效时间
        mmdQuotationDao.updateValidTime(eo.getQuotationNo(), eo.getValidStartTime(), eo.getValidEndTime());

//        String mycName = "";
//        if ("4".equals(eo.getChannelNo())) {
//            mycName = "DHL";
//        }else if ("5".equals(eo.getChannelNo()) || "6".equals(eo.getChannelNo())) {
//            mycName = "UPS";
//        }else if ("7".equals(eo.getChannelNo())) {
//            mycName = "FedEx";
//        }
//        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
//        MmdModeMYC mmdModeMYC = null;
//        if ("admin".equals(userLoginContext.getLevel())){
//            mmdModeMYC = modeMYCService.getMYCByMycAndAdmin(mycName);
//        }else{
//            mmdModeMYC = modeMYCService.getMYCByMycAndUid(userLoginContext.getLevel(), userLoginContext.getUid(), mycName);
//        }
//        BigDecimal myc = BigDecimal.ZERO;
//        if (mmdModeMYC != null && mmdModeMYC.getMyc() != null){
//            myc = mmdModeMYC.getMyc();
//        }
//        BigDecimal oneAddMyc = new BigDecimal(1).add(myc);

        //删除报价单详情
        mmdQuotationDetailDao.deleteMmdQuotationDetail(eo.getQuotationNo());
        //插入报价单详情
        List<Map> detailJsonArray = JSON.parseArray(eo.getDetailJson(), Map.class);
        MmdQuotationDetail mqd = null;
        List<MmdQuotationDetailHistory> detailHistoryList = new ArrayList<>();
        for (Map detailObject : detailJsonArray) {
            mqd = new MmdQuotationDetail();
            if (!"9".equals(eo.getChannelNo())) {
                BigDecimal kg21 = detailObject.get("kg21") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg21").toString());
                BigDecimal kg23 = detailObject.get("kg23") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg23").toString());
                BigDecimal kg46 = detailObject.get("kg46") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg46").toString());
                BigDecimal kg71 = detailObject.get("kg71") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg71").toString());
                BigDecimal kg100 = detailObject.get("kg100") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg100").toString());
                BigDecimal kg101 = detailObject.get("kg101") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg101").toString());
                BigDecimal kg301 = detailObject.get("kg301") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg301").toString());
                BigDecimal kg501 = detailObject.get("kg501") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg501").toString());
                BigDecimal kg1001 = detailObject.get("kg1001") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("kg1001").toString());

                mqd.setKg21(kg21);
                mqd.setKg23(kg23);
                mqd.setKg46(kg46);
                mqd.setKg71(kg71);
                mqd.setKg100(kg100);
                mqd.setKg101(kg101);
                mqd.setKg301(kg301);
                mqd.setKg501(kg501);
                mqd.setKg1001(kg1001);
            }else{
                BigDecimal cbm1 = detailObject.get("cbm1") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm1").toString());
                BigDecimal cbm2 = detailObject.get("cbm2") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm2").toString());
                BigDecimal cbm5 = detailObject.get("cbm5") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm5").toString());
                BigDecimal cbm10 = detailObject.get("cbm10") == null ? BigDecimal.ZERO : new BigDecimal(detailObject.get("cbm10").toString());

                mqd.setCbm1(cbm1);
                mqd.setCbm2(cbm2);
                mqd.setCbm5(cbm5);
                mqd.setCbm10(cbm10);
            }
            if (detailObject.get("drawTime") != null) {
                mqd.setDrawTime(detailObject.get("drawTime").toString());
            }

            mqd.setQuotationNo(eo.getQuotationNo());
            mqd.setSonAreaId(Long.parseLong(detailObject.get("sonAreaId").toString()));
            mqd.setSonAreaName(detailObject.get("sonAreaName").toString());
            if (detailObject.get("areaRemark") != null) {
                mqd.setAreaRemark(detailObject.get("areaRemark").toString());
            }
            mmdQuotationDetailDao.insert(mqd);

            MmdQuotationDetailHistory mmdQuotationDetailHistory = new MmdQuotationDetailHistory();
            BeanUtils.copyProperties(mqd, mmdQuotationDetailHistory);
            detailHistoryList.add(mmdQuotationDetailHistory);
        }


        //删除报价单详情
        mmdQuotationHscodeDao.deleteMmdQuotationHscodeDao(eo.getQuotationNo());
        //插入附加价格
        if (!StringUtils.isEmpty(eo.getHscodeJson())) {
            logger.info("hscodeJson===>{}", eo.getHscodeJson());

            List<Map> hscodeJsonArray = JSON.parseArray(eo.getHscodeJson(), Map.class);
            MmdQuotationHscode mqh = null;
            for (Map hscodeObject : hscodeJsonArray) {
                if (hscodeObject.get("attachFee") != null) {
                    mqh = new MmdQuotationHscode();
                    BigDecimal attachFee = new BigDecimal(hscodeObject.get("attachFee").toString());
                    mqh.setQuotationNo(eo.getQuotationNo());
                    mqh.setHscode(hscodeObject.get("hscode").toString());
                    mqh.setAttachFee(attachFee);
                    mmdQuotationHscodeDao.insert(mqh);
                }

            }
        }

        MmdQuotationHistory record = new MmdQuotationHistory();
        BeanUtils.copyProperties(mq, record);
        record.setCreateTime(new Date());
        mmdQuotationHistoryMapper.insert(record);
        for (MmdQuotationDetailHistory detailHistory : detailHistoryList){
            detailHistory.setHistoryId(record.getHistoryId());
            mmdQuotationDetailHistoryMapper.insert(detailHistory);
        }

        //添加日志
        MmdLog log = new MmdLog();
        log.setOperatorUid(AdminInterceptor.getUserLoginContext().getUid());
        log.setOperatorKey(eo.getQuotationNo());
        log.setContent("修改报价单");
        log.setCreateTime(new Date());
        logDao.insertLog(log);
        return true;
    }


    @Override
    public QuotationVo userGetQuotation(String quotationNo) {
        QuotationVo vo = new QuotationVo();

        MmdQuotation mmdQuotation = mmdQuotationDao.selectByPrimaryKey(quotationNo);
        if (mmdQuotation == null) {
            throw new GameException(new CODE(9999, "报价单编码不存在"));
        }
        BeanUtils.copyProperties(mmdQuotation, vo);

        //设置报价单详情
        MmdQuotationDetail mqd = new MmdQuotationDetail();
        mqd.setQuotationNo(mmdQuotation.getQuotationNo());
        List<MmdQuotationDetail> mmdQuotationDetails = mmdQuotationDetailDao.selectByKeySelective(mqd);
        vo.setQuotationDetails(mmdQuotationDetails);

        //设置报价单附加价详情
        MmdQuotationHscode hscode = new MmdQuotationHscode();
        hscode.setQuotationNo(mmdQuotation.getQuotationNo());
        List<MmdQuotationHscode> mmdQuotationHscodes = mmdQuotationHscodeDao.selectByKeySelective(hscode);
        RouteHscodeVo hscodeVo = null;
        List<RouteHscodeVo> routeHscodeVos = new ArrayList<>();
        for (MmdQuotationHscode mmdQuotationHscode : mmdQuotationHscodes) {
            hscodeVo = new RouteHscodeVo();
            MmdHscode mmdHscode = mmdHscodeDao.selectByPrimaryKey(mmdQuotationHscode.getHscode());
            hscodeVo.setHscode(mmdHscode.getHscode());
            hscodeVo.setAttachFee(mmdQuotationHscode.getAttachFee());
            hscodeVo.setHscodeName(mmdHscode.getHscodeName());
            routeHscodeVos.add(hscodeVo);
        }
        vo.setQuotationHscodes(routeHscodeVos);
        return vo;
    }

    @Override
    public List<QuotationValidTimeVo> getValidTimeByRouteName(String routeNo, String channelNo, Long areaId) {
//        if (StringUtils.isEmpty(channelNo)) {
//            throw new GameException(new CODE(9999, "请选择渠道"));
//        }
//        if (areaId == null) {
//            throw new GameException(new CODE(9999, "请选择区域"));
//        }
//        if (StringUtils.isEmpty(routeNo)) {
//            throw new GameException(new CODE(9999, "请选择路线"));
//        }
        if (StringUtils.isEmpty(routeNo) && StringUtils.isEmpty(channelNo) && areaId == null){
            return new ArrayList<>();
        }

        String level = null;
        UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
        if ("user".equals(userLoginContext.getLevel())) {
            //用户查询时才有用
            level = userLoginContext.getUserLevel();
        }
        if (org.apache.commons.lang3.StringUtils.isBlank(userLoginContext.getUserLevel()) && "user".equals(userLoginContext.getLevel())) {
            //如果没有级别，默认查D级别
            level = "D";
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<MmdQuotation> quotationList = mmdQuotationDao.getQuotationByParams(routeNo, channelNo, areaId, level);
        List<QuotationValidTimeVo> resultList = quotationList.stream().map(mmdQuotation -> {
            QuotationValidTimeVo quotationValidTimeVo = new QuotationValidTimeVo();
            quotationValidTimeVo.setQuotationNo(mmdQuotation.getQuotationNo());

            if (mmdQuotation.getValidStartTime() != null && mmdQuotation.getValidEndTime() != null){
                String validStartTimeStr = format.format(mmdQuotation.getValidStartTime());
                String validEndTimeStr = format.format(mmdQuotation.getValidEndTime());
                quotationValidTimeVo.setValidTimeArea(validStartTimeStr + "~" + validEndTimeStr);
            }

            return quotationValidTimeVo;
        }).collect(Collectors.toList());
        return resultList;
    }

    @Override
    public Long getLastHistoryId(String quotationNo) {
        return mmdQuotationHistoryMapper.getLastHistoryId(quotationNo);
    }

    @Override
    public QuotationVo getQuotationDetail(String quotationNo) {
        MmdQuotation mmdQuotation = mmdQuotationDao.selectByPrimaryKey(quotationNo);
        QuotationVo vo = new QuotationVo();
        if (mmdQuotation == null) {
            throw new GameException(new CODE(9999, "报价单不存在"));
        }
        BeanUtils.copyProperties(mmdQuotation, vo);

        //设置报价单详情
        MmdQuotationDetail mqd = new MmdQuotationDetail();
        mqd.setQuotationNo(quotationNo);
        List<MmdQuotationDetail> mmdQuotationDetails = mmdQuotationDetailDao.selectByKeySelective(mqd);
        vo.setQuotationDetails(mmdQuotationDetails);

        //设置报价单附加价详情
        List<RouteHscodeVo> list = mmdQuotationDao.selectAllHsCode(quotationNo);
        vo.setQuotationHscodes(list);
        logger.info("start:{}",vo.getValidStartTime());
        return vo;
    }

    @Override
    public List<QuotationVo> getQuotationList() {
        List<QuotationVo> vos = new ArrayList<>();
        List<MmdQuotation> mmdQuotations = mmdQuotationDao.selectByKeySelective(new MmdQuotation());
        if (!CollectionUtils.isEmpty(mmdQuotations)) {
            MmdQuotationDetail detail = null;
            MmdQuotationHscode hscode = null;
            QuotationVo vo = null;
            for (MmdQuotation mmdQuotation : mmdQuotations) {
                vo = new QuotationVo();

                //查询详情列表
                detail = new MmdQuotationDetail();
                detail.setQuotationNo(mmdQuotation.getQuotationNo());
                List<MmdQuotationDetail> mmdQuotationDetails = mmdQuotationDetailDao.selectByKeySelective(detail);
                BeanUtils.copyProperties(mmdQuotation, vo);
                vo.setQuotationDetails(mmdQuotationDetails);

                //查询hscode列表
//                hscode = new MmdQuotationHscode();
//                hscode.setQuotationNo(mmdQuotation.getQuotationNo());
//                List<MmdQuotationHscode> mmdQuotationHscodes = mmdQuotationHscodeDao.selectByKeySelective(hscode);
//                vo.setQuotationHscodes(mmdQuotationHscodes);
                vos.add(vo);
            }
        }
        return vos;
    }
}
